package com.tsd.jxc.report.service.impl;

import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.fin.entity.JxcFinancePaymentRequestDetailExt;
import com.tsd.jxc.report.dao.JxcReportMapper;
import com.tsd.jxc.report.service.JxcReportService;
import com.tsd.jxc.report.vo.JxcBuySaleBillDetailVo;
import com.tsd.jxc.report.vo.JxcStockInOutBillInventoryVo;
import com.tsd.jxc.report.vo.JxcStockInOutBillReportVo;
import com.tsd.service.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class JxcReportServiceImpl extends BaseServiceImpl implements JxcReportService {

    @Resource
    private JxcReportMapper jxcReportMapper;

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findBuyOrderReportList(Map<String, Object> params) throws Exception {
        return jxcReportMapper.selectBuyOrderCollectReportList(params);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findBuyReturnCollectReportList(Map<String, Object> params) {
        return jxcReportMapper.selectBuyReturnCollectReportList(params);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findBuyYearReportListByPage(Map<String, Object> params, PageBean page) throws Exception {
        return jxcReportMapper.selectBuyYearReportListByPage(params, page);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findBuyReturnYearReportListByPage(Map<String, Object> params, PageBean page) {
        return jxcReportMapper.selectBuyReturnYearReportListByPage(params, page);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findBuyGoodsReportListByPage(Map<String, Object> params, PageBean page) throws Exception {
        return jxcReportMapper.selectBuyGoodsReportListByPage(params, page);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findBuyReturnGoodsReportListByPage(Map<String, Object> params, PageBean page) {
        return jxcReportMapper.selectBuyReturnGoodsReportListByPage(params, page);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findSaleOrderCollectReportList(Map<String, Object> params) throws Exception {
        return jxcReportMapper.selectSaleOrderCollectReportList(params);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findSaleReturnCollectReportList(Map<String, Object> params) {
        return jxcReportMapper.selectSaleReturnCollectReportList(params);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findSaleYearReportListByPage(Map<String, Object> params, PageBean page) throws Exception {
        return jxcReportMapper.selectSaleYearReportListByPage(params, page);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findSaleReturnYearReportListByPage(Map<String, Object> params, PageBean page) {
        return jxcReportMapper.selectSaleReturnYearReportListByPage(params, page);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findSaleGoodsReportListByPage(Map<String, Object> params, PageBean page) throws Exception {
        return jxcReportMapper.selectSaleGoodsReportListByPage(params, page);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findSaleReturnGoodsReportListByPage(Map<String, Object> params, PageBean page) {
        return jxcReportMapper.selectSaleReturnGoodsReportListByPage(params, page);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findStockInbillReportListByPage(Map<String, Object> params, PageBean page) {
        return jxcReportMapper.selectStockInbillReportListByPage(params, page);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findStockOutbillReportListByPage(Map<String, Object> params, PageBean page) {
        return jxcReportMapper.selectStockOutbillReportListByPage(params, page);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findStockInventoryReportListByPage(Map<String, Object> params, PageBean page) {
        return jxcReportMapper.selectStockInventoryReportListByPage(params, page);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findBuyAmountQtyVisualChartList(Map<String, Object> params) {
        return jxcReportMapper.selectBuyAmountQtyVisualChartList(params);
    }

    @Override
    public LinkedList<LinkedHashMap<String, Object>> findSaleAmountQtyVisualChartList(Map<String, Object> params) {
        return jxcReportMapper.selectSaleAmountQtyVisualChartList(params);
    }

    @Override
    public List<JxcStockInOutBillReportVo> findJxcStockInOutBillReport(Map<String, Object> params, PageBean page) throws Exception {
        String type = HlpUtils.getString(params, "type");
        if (!Arrays.asList(JxcFinancePaymentRequestDetailExt.SRC_TYPE_IN_BILL, JxcFinancePaymentRequestDetailExt.SRC_TYPE_OUT_BILL).contains(type)) {
            throw new HlpException("参数type值错误，有效值：in_bill、out_bill");
        }
        params.put("table_name", JxcFinancePaymentRequestDetailExt.SRC_TYPE_IN_BILL.equals(type) ? "vw_report_stock_in_bill_item" : "vw_report_stock_out_bill_item");
        String clientSupplier = HlpUtils.getString(params, "client_supplier");
        String goodsSpec = HlpUtils.getString(params, "goods_spec");
        List<String> groupList = new ArrayList<>();
        if (!"-1".equals(clientSupplier)) {
            groupList.add(clientSupplier);
        }
        if (!"-1".equals(goodsSpec)) {
            groupList.add(goodsSpec);
        }
        if (groupList.contains("client_sid")) {
            params.put("client_not_empty", "1");
        }
        if (groupList.contains("supplier_sid")) {
            params.put("supplier_not_empty", "1");
        }
        params.put("group_by", " GROUP BY " + StringUtils.join(groupList, ", "));
        List<Integer> monthList = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            monthList.add(i);
        }
        params.put("month_list", monthList);
        return jxcReportMapper.selectJxcStockInOutBillReportByPage(params, page);
    }

    @Override
    public List<JxcStockInOutBillInventoryVo> findJxcStockInOutBillInventory(Map<String, Object> params, PageBean page) throws Exception {
        String type = HlpUtils.getString(params, "type");
        if (!Arrays.asList(JxcFinancePaymentRequestDetailExt.SRC_TYPE_IN_BILL, JxcFinancePaymentRequestDetailExt.SRC_TYPE_OUT_BILL).contains(type)) {
            throw new HlpException("参数type值错误，有效值：in_bill、out_bill");
        }
        params.put("table_name", JxcFinancePaymentRequestDetailExt.SRC_TYPE_IN_BILL.equals(type) ? "vw_report_stock_in_bill_item" : "vw_report_stock_out_bill_item");
        return jxcReportMapper.selectJxcStockInOutBillInventoryByPage(params, page);
    }

    @Override
    public List<JxcBuySaleBillDetailVo> findBuySaleBillDetailReport(Map<String, Object> params, PageBean page) {
        return jxcReportMapper.selectBuySaleBillDetailReport(params, page);
    }
}
